#ifndef USERHANDLE_H
#define USERHANDLE_H

#include <QObject>
#include "opencv.h"
#include "file.h"
#include "http.h"
#include "effectedit.h"

class userhandle : public QObject
{
    Q_OBJECT
public:
    explicit userhandle(QObject *parent = nullptr);
    ~userhandle();
    http *m_http;

    void setLedNum(int ledNum);
    int getLedNum();
    void setValidFrameNum(int validFrameNum);
    int getValidFrameNum();
    void setFrameRate(int frameRate);
    int getFrameRate();
    void setFrameWidth(int frameWidth);
    int getFrameWidth();
    void setFrameHeight(int frameHeight);
    int getFrameHeight();

    Q_INVOKABLE void setDeviceState(bool state);

    Q_INVOKABLE QVariant getFileList();
    Q_INVOKABLE bool setLedColor(int ledPosition, QColor color);
    Q_INVOKABLE QColor getLedColor(int ledPosition);
    Q_INVOKABLE void setMyProperty(QString property, QVariant value);
    Q_INVOKABLE QVariant getMyProperty(QString property);

    Q_INVOKABLE void importAndOpenVideo(int mode);
    Q_INVOKABLE void processVideo(int mode);
    Q_INVOKABLE void importAndOpenLDFile();
    Q_INVOKABLE void processLDFile();
    Q_INVOKABLE void importAndOpenFrames(int mode);
    Q_INVOKABLE void processFrames(int mode);
    Q_INVOKABLE bool createVideoFileLD();

    Q_INVOKABLE bool uploadLedEffect(QString ledEffectNum);
    Q_INVOKABLE void getDeviceCANID(QString mode);
    Q_INVOKABLE void setDeviceCANID(QString id, QString mode);
    Q_INVOKABLE void getDeviceVersions();
    Q_INVOKABLE bool strContains(QString src, QString target);
    Q_INVOKABLE QStringList httpResultHandle(QString ret, bool mode);
    Q_INVOKABLE void updateDeviceVersions();
    Q_INVOKABLE void previewLedEffect(QString effectID);
    void getHeartbeat();

    Q_INVOKABLE bool saveOrCutFrame(int start, int end, QString mode);

    Q_INVOKABLE void oneColor_create(QColor color);
    Q_INVOKABLE void sectionsRWL_create(int rwLedNum, int rwSpacing, int rwSections, int rwDelayFrame, QColor rwColor, QColor backColor);

signals:
    void videoFileOpened(QString filePath);
    void videoFileProcessed();
    void ldFileOpened();
    void ldFileProcessed();
    void framesOpened(int fileCount);
    void framesProcessed();
    void deviceStateChanged(bool state);
    void tipsShow(QString tips);

public slots:
    void heartbeatHandle(QString ret);

private:
    opencv *m_opencv;
    file *m_file;
    effectedit *m_effectedit;
    QList<QImage> m_imgData;
    QList<QColor> m_colorData;
    int m_ledNum = 0;
    int m_validFrameNum = 0;
    int m_frameRate = 30;
    int m_frameWidth = 0;
    int m_frameHeight = 0;
    bool m_deviceState = false;

    QList<QColor> colorCalcAllFrame(int mode);
    QList<QColor> colorCalcOneFrame(QImage img, int ledNum, int width, int height, int mode);
    QByteArray createConfigData(uchar chX, ushort ch1Num, ushort ch2Num, ushort ch1Frames, ushort ch2Frames);
    QByteArray QColorToQByteArray();
    QList<QColor> QByteArrayToQColor(QByteArray byteArr);
};

#endif // USERHANDLE_H
